package com.huang;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.junit.jupiter.api.Test;


import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;


/**
 * @author HuangShen
 * @Description
 * @create 2021-03-31 13:56
 */
public class KaiserDemo {
    @Test
    public void  test1(){
        String s="HelloWord";
        int key=3;
        StringBuilder stringBuilder = getStringBuilder(s, key);

        System.out.println(stringBuilder);



    }


    private StringBuilder getStringBuilder(String s, int key) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            int x=aChar;
            x=x+ key;
            stringBuilder.append((char)x);

        }
        return stringBuilder;
    }


    @Test
    public void testDES() throws Exception {
        // 原文
        String input = "硅谷";
        // des加密必须是8位
        String key = "12345678";
        // 算法
        String algorithm = "DES";

        String transformation = "DES";
        // Cipher：密码，获取加密对象
        // transformation:参数表示使用什么类型加密
        Cipher cipher = Cipher.getInstance(transformation);
        // 指定秘钥规则
        // 第一个参数表示：密钥，key的字节数组
        // 第二个参数表示：算法
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        // 对加密进行初始化
        // 第一个参数：表示模式，有加密模式和解密模式
        // 第二个参数：表示秘钥规则
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        // 进行加密
        byte[] bytes = cipher.doFinal(input.getBytes());
        // 打印字节，因为ascii码有负数，解析不出来，所以乱码
        for (byte b : bytes) {
            System.out.println(b);
        }
        // 打印密文
//        System.out.println(new String(bytes));
    }


    @Test
    public void testAESEncode(){
        //加密明文
        String input="这是明文";
        //加密钥 AES key 必须16字节
        String key="1234567812345678";
        //加密算法
        String algorithm="AES";
        //加密算法与填充模式
        String transformation = "AES";

        try {
            Cipher cipher = Cipher.getInstance(transformation);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] bytes = cipher.doFinal(input.getBytes());
            String encode = Base64.encode(bytes);
            System.out.println(encode);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @Test
    public void testAESDecode(){
        //加密密文
        String input="pId7prEghHqC5Teugzwwig==";
        //加密钥 AES key 必须16字节
        String key="1234567812345678";
        //加密算法
        String algorithm="AES";
        //加密算法与填充模式
        String transformation = "AES";

        try {

            Cipher cipher = Cipher.getInstance(transformation);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] bytes = cipher.doFinal(Base64.decode(input));
            String s = new String(bytes);
            System.out.println(s);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
